<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cookie高级封装</title>
</head>
<body>
    <script>
        /* // 初级封装
        function setCookie(name,value,options){
            var cookie_str = name+'='+value;
            // 是否存在过期时间
            if(typeof options.expires==="number"){
                var t = new Date();
                t.setDate(t.getDate()+options.expires);
                cookie_str += ";expires="+t;
            }
            // 是否存在路径
            if(options.path){
                cookie_str += ";path="+options.path;
            }
            // 是否存在域名
            if(options.domain){
                cookie_str += ";path="+options.domain;
            }
            document.cookie = cookie_str;
        } 
        setCookie('username','xiaohong',{
            expires:3,
            path:'/',
            domain : "localhost"
        }) */

        // 高级封装：利用数组数据能进行运算和数组字符串间能自由转换的特性来写封装
        function setCookie(name,value,opstion){

            if(typeof opstion.expires === "number"){
                var t = new Date();
                t.setDate(t.getDate()+opstion.expires);
            }

            var cookie_arr = [
                name+'='+value,
                typeof opstion.expires === "number"?';expires='+t:'',
                typeof opstion.path === "string"?';path='+opstion.path:'',
                typeof opstion.domain === "string"?';domain='+opstion.domain:''
            ]

            return document.cookie = cookie_arr.join('');
        }

        
    </script>
</body>
</html>